using System;
using System.Collections.Generic;
using System.Text;
using Guestbook.domain;
using Guestbook.business.service;
using System.Web.Mail;

namespace Guestbook.Page
{
    public class AdminLoginPage : BasePage
    {
        #region - Fields & Properties -

        protected System.Web.UI.WebControls.HyperLink lnkBack;

        protected System.Web.UI.WebControls.Button btnLogin;
        protected System.Web.UI.WebControls.Button btnGetInfo;
        protected System.Web.UI.WebControls.TextBox txtPassword;

        public int GuestBookId
        {
            get
            {
                string objPage = Request["id"];
                return int.Parse(objPage);
            }
        }

        public string Password
        {
            get { return txtPassword.Text.Trim(); }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                bool isExist = Guestbook.business.service.GuestBookService.IsExist(this.GuestBookId);
                if (isExist)
                {
                    lnkBack.NavigateUrl = string.Format("/guestbook.aspx?id={0}", this.GuestBookId);
                }
            }
            catch (Exception)
            {

                Response.Redirect("/");
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                GuestBook obj = GuestBookService.GetSelfGuestBookById(this.GuestBookId);
                if (obj != null && obj.UserPassword == this.Password)
                    Response.Redirect(string.Format("/guestbook.aspx?id={0}", this.GuestBookId), false);
            }
            catch (Exception)
            {
                Response.Redirect("/");
            }
        }

        protected void btnGetInfo_Click(object sender, EventArgs e)
        {
            try
            {
                GuestBook obj = GuestBookService.GetSelfGuestBookById(this.GuestBookId);

                string strTo = obj.UserEmail;
                string strFrom = "webmaster@xxx.com";
                string strSubject = "ABCDE";


                GuestBookService.SendMail(strFrom, strTo, strSubject, "TEST");
            }
            catch (Exception ex)
            {
                //todo
            }
        }
    }
}
